package com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityMapData.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityMapData.entity.SeiIntMapData;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityMapData.queryParams.QueryParams;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityMapData.repository.SeiIntMapDataRepository;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityMapData.vo.SeiIntMapDataVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SeiIntMapDataRepositoryImpl implements SeiIntMapDataRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

   /* @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String eventName) {
        StringBuilder sql = new StringBuilder("select a.xsem_id,a.event_code,a.event_name,b.id,b.expert_opinion,b.expert_opinion_pdf,b.seismic_intensity_map " +
                "from xczh_seismic_event_management a left join xczh_seismic_intensity_map_data b on a.xsem_id=b.xsem_id");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        if (!PlatformObjectUtils.isEmpty(eventName)) {
            whereSql.append(" and a.event_name like '%" + eventName + "%'");
        }
  *//*      if (!PlatformObjectUtils.isEmpty(queryParams.getXsemId())) {
            whereSql.append(" and a.xsem_id = '" + queryParams.getXsemId() + "'");
        }
*//*
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(EarEveManage.class)).size();
        List<SeiIntMapDataVo> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SeiIntMapDataVo.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;


    }*/
    @Override
    public void save(SeiIntMapData seiIntMapData) {
        String sql="insert into xczh_seismic_intensity_map_data(id,expert_opinion,expert_opinion_pdf,seismic_intensity_map,process_materials,xsem_id) " +
                "values(:id, :expertOpinion, :expertOpinionPdf, :seismicIntensityMap, :processMaterials, :xsemId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(seiIntMapData);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public SeiIntMapData findByXsemId(String xsemId) {
        String sql="select * from xczh_seismic_intensity_map_data where xsem_id = ? ";
        SeiIntMapData seiIntMapData=new SeiIntMapData();
        try {
            seiIntMapData = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SeiIntMapData.class), xsemId);
            return seiIntMapData;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void update(SeiIntMapData seiIntMapData) {
        String sql="update xczh_seismic_intensity_map_data set expert_opinion = :expertOpinion ,expert_opinion_pdf = :expertOpinionPdf ," +
                "seismic_intensity_map = :seismicIntensityMap ,process_materials = :processMaterials ,xsem_id = :xsemId where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(seiIntMapData);
        namedParameterJdbcTemplate.update(sql, source);
    }
}
